﻿/***************************
* 1.游客列表
****************************/

/* 变量 */
var touristList = "booking/touristList.html";

//游客列表
var contactsCallback_touristList = myApp.onPageInit('touristList', function (page) {
    var _userID = iss_localStorage.get("yuqing_appUserId"); // 全局变量
    //初始化加载全部数据
    loadData();
    function loadData() {
        var _content = JSON.stringify({
            VisitorsEntity: {
                UserID: _userID,
                IsDelete: 0
            },
            type: 60
        });
        myApp.showPreloader();
        $$.ajax({
            url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.ATTAR,
            data: { "content": _content },
            crossDomain: true,
            method: "POST",
            success: function (data, status) {
                try {
                    data = JSON.parse(data);
                    if (data && data.status == "0" && data.results) {
                        var searchTemplate = $$('#template-tourist-tab1').html();
                        if (searchTemplate) {

                            //处理勾选
                            var checklist = [];
                            //门票预订
                            if (page.fromPage.name == "ticketBooking") {
                                $$(page.fromPage.container).find(".tourist ul li").each(function () {
                                    var value = $$(this).attr("value");
                                    checklist.push(value);
                                });
                            }
                            for (var i = 0; i < data.results.length; i++) {
                                data.results[i].ischecked = ""; //页面模板需要
                                for (var j = 0; j < checklist.length; j++) {
                                    if (data.results[i].ID == checklist[j]) {
                                        data.results[i].ischecked = "true";
                                        break;
                                    }
                                }
                            }

                            //绑定数据
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#touristList_page .list-block ul#list").html(html);

                            myApp.hidePreloader();
                        }
                    }
                } catch (ex) {
                    myApp.alert('获取游客失败!');
                    myApp.hidePreloader();
                }
            },
            error: function (xhr, status) {
                myApp.alert('获取游客失败!');
                myApp.hidePreloader();
                console.log("ajax error!");
            }
        });
    }

    //确定事件
    $$("#touristList_btn_subcheck").click(function () {
        //门票预订
        if (page.fromPage.name == "ticketBooking") {
            var li_html = "";
            $$(".list-block ul li input[name='my-checkbox']:checked").each(function () {
                var lable = $$(this).parent("label"); //父级lable
                var e_ul = $$(page.fromPage.container).find(".tourist_hide ul");
                $$(e_ul).find(".item-title").html($$(lable).find(".item-title").html());
                $$(e_ul).find(".item-after").html($$(lable).find(".item-after").html());
                $$(e_ul).find("li").attr("value", $$(lable).attr("value"));
                li_html += $$(e_ul).html();
            });
            $$(page.fromPage.container).find(".tourist ul").html(li_html);

        }
        mainView.router.back();
    })

    //新增
    $$("#touristList_page a.add-tourist").click(function () {
        $$("#hid_tourist_id").val("");
        myApp.popup(".popup-edit");
    })
    //保存
    $$("#btn_popup_save").click(function () {
        var tourist_id = $$("#hid_tourist_id").val();
        if (tourist_id == "") {
            //新增
            var name = $$("#txt_tourist_name").val();
            var idcard = $$("#txt_tourist_idcard").val();
            if (name == "") {
                myApp.alert('请输入姓名!');
                return;
            }
            if (!(/^[\u4e00-\u9fa5]{2,4}$/).test(name)) {//匹配2到四个汉字  
                myApp.alert('请输入2到4个字的中文姓名!');
                return;
            }
            if (idcard == "") {
                myApp.alert('请输入身份证号!');
                return;
            }
            var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if (reg.test(idcard) === false) {
                myApp.alert('身份证输入不合法!');
                return;
            }
            var _content = JSON.stringify({
                VisitorsEntity: {
                    UserName: name,
                    IDCard: idcard,
                    UserID: _userID,
                    CreatePerson: iss_localStorage.get("yuqing_appNickName"),
                    CreateDate: ISS.utils.dateToStr("yyyy-MM-dd HH:mm:ss", new Date()),
                    ModifyPerson: iss_localStorage.get("yuqing_appNickName"),
                    ModifyDate: ISS.utils.dateToStr("yyyy-MM-dd HH:mm:ss", new Date())
                },
                type: 61
            });
            myApp.showPreloader();
            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.ATTAR,
                data: { "content": _content },
                crossDomain: true,
                method: "POST",
                success: function (data, status) {
                    try {
                        data = JSON.parse(data);
                        if (data && data.status == "0") {
                            myApp.hidePreloader();
                            myApp.closeModal(".popup-edit");
                            myApp.alert('保存成功!', function () {
                                loadData();
                            });
                        }
                        else {
                            myApp.alert('保存游客信息失败!');
                            myApp.hidePreloader();
                            return;
                        }
                    } catch (ex) {
                        myApp.alert('保存游客信息失败!');
                        myApp.hidePreloader();
                        return;
                    }
                },
                error: function (xhr, status) {
                    myApp.alert('保存游客信息失败!');
                    myApp.hidePreloader();
                    console.log("ajax error!");
                    return;
                }
            });
        }

    })

    //删除
    $$("#touristList_page ul#list").on("click", "a.item-edit", function () {
        var id = $$(this).attr("value");
        if (id != "") {
            var li = $$(this).parent("li");
            myApp.confirm('确定删除?', function () {
                myApp.showPreloader();
                var _content = JSON.stringify({
                    VisitorsEntity: {
                        ID: id
                    },
                    type: 62
                });
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.ATTAR,
                    data: { "content": _content },
                    crossDomain: true,
                    method: "POST",
                    success: function (data, status) {
                        try {
                            data = JSON.parse(data);
                            if (data && data.status == "0") {
                                myApp.hidePreloader();
                                myApp.alert('删除成功!');
                                $$(li).remove();

                            }
                            else {
                                myApp.alert('删除失败!');
                                myApp.hidePreloader();
                                return;
                            }
                        } catch (ex) {
                            myApp.alert('删除失败!');
                            myApp.hidePreloader();
                            return;
                        }
                    },
                    error: function (xhr, status) {
                        myApp.alert('删除失败!');
                        myApp.hidePreloader();
                        console.log("ajax error!");
                        return;
                    }
                });
            });
        }
    });

});


/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(touristList) > -1) {
        contactsCallback_touristList.trigger();
    }
}

initData();